/**
 * Date Created: Apr 12, 2009
 */
package forteresce.portprofile.gui;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;

import org.apache.log4j.Logger;

import forteresce.portprofile.config.constants.ProfileConstants;
import forteresce.portprofile.gui.dialogs.PortProfileDialogs;
import forteresce.portprofile.gui.events.ShowProfilesEvent;
import forteresce.portprofile.gui.events.ShowWaitEvent;
import forteresce.portprofile.gui.panels.ProfilesExportPanel;
import forteresce.portprofile.gui.panels.ProfilesImportPanel;
import forteresce.portprofile.gui.panels.WaitPanel;
import forteresce.portprofile.config.enums.SystemPropertiesEnum;

public class PortProfileFrame extends JFrame {
	private static Logger log = Logger.getLogger(PortProfileFrame.class);

	private ProfilesExportPanel profilesExportTab;
	private ProfilesImportPanel profilesImportTab;
	private WaitPanel waitPanel;
	private CardLayout cardLayout;

	private static final String HOW_TO = "How To";
	private static final String ABOUT = "About";


	private static final long serialVersionUID = 8165919849579765249L;

	public PortProfileFrame() {
		super("PortProfile");

		//verify java version
		if (!verifyJavaVersion()) {
			PortProfileDialogs.showError(this, "Portprofile requires java version 1.6+.");
			System.exit(-1);
		}

		//set the regular properties
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);

		//set the dimensions and location
		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
		setMinimumSize(ProfileConstants.MAIN_PANEL_DIM);
		setLocation((screenDimension.width - ProfileConstants.MAIN_PANEL_DIM.width) / 2,
                (screenDimension.height - ProfileConstants.MAIN_PANEL_DIM.height) / 2);

		if(null != PortProfileDialogs.class.getResource("/portprofile_icon_transparent.png")) {
			setIconImage(new ImageIcon(PortProfileDialogs.class.getResource("/portprofile_icon_transparent.png")).getImage());
		}

		//set the layout and the panels
		createPanels();

		//create the menus
		createMenus();

		//a component listener to handle the showwaitevent
		addComponentListener(new CardShowListener());

	}

	private void createMenus() {
		JMenuBar menuBar = new JMenuBar();

		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);

		MenuListener menuListener = new MenuListener(this);

		JMenuItem about = new JMenuItem(ABOUT);
		about.setMnemonic(KeyEvent.VK_A);
		about.addActionListener(menuListener);

		JMenuItem howto = new JMenuItem(HOW_TO);
		howto.setMnemonic(KeyEvent.VK_W);
		howto.addActionListener(menuListener);

		helpMenu.add(about);
		helpMenu.add(howto);
		menuBar.add(helpMenu);

		setJMenuBar(menuBar);
	}

	private void createPanels() {
		cardLayout = new CardLayout();
		this.getContentPane().setLayout(cardLayout);

		//wait panel
		waitPanel = new WaitPanel();
		add(waitPanel, ProfileConstants.WAIT_CARD);

		//profile panels
		profilesExportTab = new ProfilesExportPanel(this);
		profilesImportTab = new ProfilesImportPanel(this);

		//tabbed pane for profiles
		JTabbedPane profilesPane = new JTabbedPane();
		profilesPane.addTab("Export", profilesExportTab);
		profilesPane.setMnemonicAt(0, KeyEvent.VK_E);
		profilesPane.addTab("Import", profilesImportTab);
		profilesPane.setMnemonicAt(1, KeyEvent.VK_I);

		add(profilesPane, ProfileConstants.PROFILES_CARD);
	}

	/**
	 * Mainly used to default the display to the profiles panel
	 */
	public void init() {
		try {
			profilesExportTab.loadProfileValues();
			cardLayout.show(this.getContentPane(), ProfileConstants.PROFILES_CARD);
		} catch (Exception e) {
			PortProfileDialogs.showError(this, "There was an error initializing profile.");
			log.error("Error in init.", e);
			System.exit(-1);
		}
	}

	/**
	 * An adapter to listen to the cardshowevent
	 * @author forteresce
	 */
	class CardShowListener extends ComponentAdapter {
		@Override
		public void componentShown(ComponentEvent event) {
			if(event instanceof ShowWaitEvent) {
				waitPanel.setMessage(((ShowWaitEvent) event).getMessage());
				cardLayout.show(((JFrame) event.getSource()).getContentPane(), ProfileConstants.WAIT_CARD);
			} else if(event instanceof ShowProfilesEvent) {
				cardLayout.show(((JFrame) event.getSource()).getContentPane(), ProfileConstants.PROFILES_CARD);
			}
		}
	}

	/**
	 * MenuListener for the menu bar
	 * @author forteresce
	 *
	 */
	class MenuListener implements ActionListener {
		JFrame parentFrame;

		public MenuListener(JFrame frame) {
			this.parentFrame = frame;
		}

		public void actionPerformed(ActionEvent event) {
			JMenuItem menuItem = (JMenuItem) event.getSource();

			if(ABOUT.equals(menuItem.getText())) {
				PortProfileDialogs.showAbout(parentFrame);
			} else if (HOW_TO.equals(menuItem.getText())) {
				PortProfileDialogs.showHowto(parentFrame);
			}
		}
	}
	
	/**
	 * Checks for the java version and returns true if 1.6 or greater 
	 */
	private boolean verifyJavaVersion() {
		String version = SystemPropertiesEnum.JAVA_VERSION.get();
		float versionNumber = Float.parseFloat(version.substring(0, 3));

	    log.debug("java version: " + versionNumber);
		if (versionNumber < 1.6 ) {
			return false;
		}

		return true;
	}
}